package cn.com.fssg.platform.base.handler.impl.gsl.lz;

import cn.com.fssg.platform.base.handler.InterfaceHandler;
import cn.com.fssg.platform.biz.BizUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description: 工商联履职创建接口
 */
@Component
public class GslLzSaveCtl implements InterfaceHandler {

    private NamedParameterJdbcTemplate jdbcTemplate = BizUtil.cur().tenantMainJdbcTemplate();

//    @Autowired
//    private NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object run(HttpServletRequest request, JSONObject jsonObject) throws Exception {
        String id = StringUtils.isEmpty(jsonObject.getString("id")) ? BizUtil.cur().getSnowflakeIdStr() : jsonObject.getString("id");;
        jsonObject.put("id", id);
        //保存履职考核信息
        return saveAccessment(jsonObject);
    }

    public int saveAccessment(JSONObject jsonObject) {
        //校验是否存在该条数据
        String sql = "select * from gsl_assessment where id=:id";
        Map<String, Object> param = new HashMap<>();
        param.put("id", jsonObject.getString("id"));
        List<Map<String, Object> > accList = jdbcTemplate.queryForList(sql, param);

        //封装保存参数
        Map<String, Object> saveMap = new HashMap<>();
        saveMap.put("id", jsonObject.getString("id"));
        saveMap.put("assessment_name", jsonObject.getString("assessment_name"));
        saveMap.put("assessment_date", jsonObject.getString("assessment_date"));
        saveMap.put("end_date", jsonObject.getString("end_date"));
        saveMap.put("count", "0");
        saveMap.put("state", "-1");
        saveMap.put("del_flag", "0");


        saveMap.put("update_by", BizUtil.cur().curUserId());
        saveMap.put("create_by", BizUtil.cur().curUserId());
        saveMap.put("update_date", new Date());
        saveMap.put("create_date", new Date());
        saveMap.put("role_scope", "1");
        saveMap.put("parent_role_scope", "1");

        int count = 0;
        if(accList.isEmpty()) {
            //新增
            String insertSql = "insert into gsl_assessment (" +
                    "id, assessment_name, state, count, end_date, update_by, update_date, create_by, create_date, del_flag, " +
                    "assessment_date, role_scope, parent_role_scope) values " +
                    "(:id, :assessment_name, :state, :count, :end_date, :update_by, :update_date, :create_by, :create_date, :del_flag, " +
                    ":assessment_date, :role_scope, :parent_role_scope)";
            count = jdbcTemplate.update(insertSql, saveMap);
        } else {
            String updateSql = "update gsl_assessment set " +
                    "assessment_name=:assessment_name, " +
                    "assessment_date=:assessment_date, " +
                    "end_date=:end_date, " +
                    "update_by=:update_by, " +
                    "update_date=:update_date, " +
                    "role_scope=:role_scope, " +
                    "parent_role_scope=:parent_role_scope where id=:id";
            count = jdbcTemplate.update(updateSql, saveMap);
        }
        saveAccessmentDetail(jsonObject);
        return count;
    }

    public int saveAccessmentDetail(JSONObject jsonObject) {
        //将历史数据设置为删除
        String deleteSql = "update gsl_assessment_subject set del_flag=1 where assessment_id=:id";
        jdbcTemplate.update(deleteSql, Collections.singletonMap("id", jsonObject.getString("id")));
        String mainId = jsonObject.getString("id");
        JSONArray jsonarray = jsonObject.getJSONArray("assList");
        List<Map<String, Object>> insertParam = new ArrayList<>();
        String insertSql = "INSERT INTO gsl_assessment_subject " +
                "(id, assessment_id, subject_name, subject_tips, subject_fraction, repeat_count, sort, " +
                "update_by, create_by, update_date, create_date, role_scope, parent_role_scope, audit_state, del_flag) " +
                "VALUES " +
                "(:id, :assessment_id, :subject_name, :subject_tips, :subject_fraction, :repeat_count, :sort, " +
                ":update_by, :create_by, :update_date, :create_date, :role_scope, :parent_role_scope, :audit_state, :del_flag)";
        AtomicInteger count = new AtomicInteger();
        jsonarray.forEach(item -> {
            JSONObject itemObj = (JSONObject) item;
            Map<String, Object> insertMap = new HashMap<>();
            insertMap.put("id", BizUtil.cur().getSnowflakeIdStr());
            insertMap.put("assessment_id", mainId);
            insertMap.put("subject_name", itemObj.get("subjectName"));
            insertMap.put("subject_tips", itemObj.get("subjectTips"));
            insertMap.put("subject_fraction", itemObj.get("subjectFraction"));
            insertMap.put("repeat_count", itemObj.get("repeatCount"));
            insertMap.put("sort", itemObj.get("sort"));

            insertMap.put("update_by", BizUtil.cur().curUserId());
            insertMap.put("create_by", BizUtil.cur().curUserId());
            insertMap.put("update_date", new Date());
            insertMap.put("create_date", new Date());
            insertMap.put("role_scope", "1");
            insertMap.put("parent_role_scope", "1");
            insertMap.put("audit_state", "0");
            insertMap.put("del_flag", "0");
            if(Objects.isNull(itemObj.get("repeatCount"))) {
                insertMap.put("repeat_count", 1);
            }
            count.set(jdbcTemplate.update(insertSql, insertMap));
        });
        return count.get();
    }
}